package com.yupi.springbootinit.utils;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName: com.xinmei.oc.util.AutoIncrementer
 * @Description: 自动生成增量的有序生成器
 * @author gdw
 * @date 2017年9月12日 下午2:08:55
 */
public class AutoIncrementer extends AtomicInteger{
	private static final long serialVersionUID = -7062647241554741513L;

	private int maxValue;
	
	private int minValue;
	
	public AutoIncrementer(int minValue, int maxValue) {
		super(minValue);
		this.maxValue = maxValue;
		this.minValue = minValue;
	}

	public final int increment() {
        for (;;) {
            int current = get();
            int next = current == maxValue ? minValue : current + 1;
            if (compareAndSet(current, next))
                return next;
        }
    }
}
